package com.interfaces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.util.framework.BaseController;

@Controller
public class VisaMockController extends BaseController {
	protected Logger log = Logger.getLogger(this.getClass());

	public static final String URL_ADD = "/visaMock/add";
	public static final String URL_QUERY = "/visaMock/query";
	public static final String URL_QUERY2 = "/visaMock/query2";

	@RequestMapping(value = URL_ADD)
	public ModelAndView queryData(@RequestParam(value = "_PAGE_KEY", required = false) String pageKey,
			@RequestParam(value = "BTN_CHECK_x", required = false) String submit,
			@RequestParam(value = "BTN_CHECK_SUBMIT_x", required = false) String confirm, HttpServletRequest request,
			HttpServletResponse response) {

		log.info("pageKey==>" + pageKey);
		log.info("submit==>" + submit);
		log.info("confirm==>" + confirm);

		ModelAndView mav = new ModelAndView();
		if (submit != null) {
			mav.setViewName("7");
		} else if (confirm != null) {
			mav.setViewName("8");
		} else {
			mav.setViewName("6");
		}

		
		try {
			// print body -- 根据servlet规范, 必须在调用request.getParameters之前
			log.info("-------------------body data--------------------------");
			BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				log.info(line);
			}

			// print parameters
			log.info("-------------------parameters--------------------------");
			Enumeration<String> paramNames = request.getParameterNames();
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues != null) {
					for (String param : paramValues) {
						log.info(paramName+":"+param );
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = URL_QUERY, produces = { "application/json;charset=utf-8" })
	public String queryData2(String CHINA_AGENT_CODE) {

		log.info("queryData2===>" + CHINA_AGENT_CODE);
		if (CHINA_AGENT_CODE != null) {
			return "{\n" + "    \"CHINA_AGENT_ID\": \"171\",\n" + "    \"COMPANY_NAME\": \"寧波浙侖海外旅行社有限公司\",\n"
					+ "    \"DIPLOMAT_NAME\": \"上海\"\n" + "}";
		} else {
			return null;
		}

	}

	@ResponseBody
	@RequestMapping(value = URL_QUERY2, produces = { "application/json;charset=utf-8" })
	public String queryData3(HttpSession session) {

		log.info("queryData3");
		return "{\n" + "    \"CHINA_AGENT_ID\": \"171\",\n" + "    \"COMPANY_NAME\": \"寧波浙侖海外旅行社有限公司\",\n"
				+ "    \"DIPLOMAT_NAME\": \"上海\"\n" + "}";
	}
}
